﻿/*************************************
Function：工具类
 Version：1.0
    Date：2012-04-28
  Author：rjdong
   Email：ooxxab@gmail.com
 WebSite：http://emake.info
*************************************/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;

using Wind.Compressor.Entity;


namespace Wind.Compressor.Business
{
    public class Utils
    {
        const string ConfigName = "config.xml";
        /// <summary>
        /// 保存设置项到XML文件
        /// </summary>
        /// <param name="setting"></param>
        public static void SaveSetting(SettingInfo setting)
        {
            try
            {
                SerializerHelper.XmlSerializerToFile(setting, Application.StartupPath + "\\" + ConfigName);
            }
            catch (Exception ex)
            {
                LogHelper.Write("Wind.Compressor.Business.Utils.SaveSetting error", ex);
            }
        }
        /// <summary>
        /// 读取设置从XML文件
        /// </summary>
        /// <returns></returns>
        public static SettingInfo LoadSetting()
        {
            SettingInfo settingInfo = null;
            try
            {
                string path = Application.StartupPath + "\\" + ConfigName;
                if (File.Exists(path))
                    settingInfo = (SettingInfo)SerializerHelper.XmlDeserializerFormFile(typeof(SettingInfo), path);
            }
            catch (Exception ex)
            {
                LogHelper.Write("Wind.Compressor.Business.Utils.LoadSetting error", ex);
            }
            return settingInfo;
        }

        public static Encoding GetEncoding(string encodingName)
        {
            Encoding encoding = null;
            try
            {
                if (encodingName == "保持原文件编码")
                {
                    return null;
                }
                if (!string.IsNullOrEmpty(encodingName))
                {
                    bool isFind = false;
                    EncodingInfo[] encodes = Encoding.GetEncodings();
                    foreach (EncodingInfo item in encodes)
                    {
                        if (encodingName.ToLower() == item.DisplayName.ToLower())
                        {
                            encoding = item.GetEncoding();
                            isFind = true;
                            break;
                        }
                    }
                    if (!isFind)
                    {
                        encoding = Encoding.UTF8;
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.Write("Wind.Compressor.Business.Utils.GetEncoding error", ex);
            }
            return encoding;
        }

        #region File Operation Mothed
        /// <summary>
        /// 读文件
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static string ReadFile(string fileName)
        {
            string result = string.Empty;
            try
            {
                using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    using (StreamReader sr = new StreamReader(fs, Encoding.Default))
                    {
                        result = sr.ReadToEnd();
                        sr.Close();
                    }
                    fs.Close();
                }
            }
            catch (Exception ex)
            {
                LogHelper.Write("Wind.Compressor.Business.Utils.ReadFile error", ex);
            }
            return result;
        }
        /// <summary>
        /// 读文件
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="encoding"></param>
        /// <returns></returns>
        public static string ReadFile(string fileName, out Encoding encoding)
        {
            string result = string.Empty;
            encoding = null;
            try
            {
                using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    using (StreamReader sr = new StreamReader(fs))
                    {
                        result = sr.ReadToEnd();
                        encoding = sr.CurrentEncoding;
                        sr.Close();
                    }
                    fs.Close();
                }
            }
            catch (Exception ex)
            {
                LogHelper.Write("Wind.Compressor.Business.Utils.ReadFile error", ex);
            }
            return result;
        }
        /// <summary>
        /// 写文件
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="content"></param>
        public static void WriteFile(string fileName, string content, Encoding encoding)
        {
            try
            {
                string path = Path.GetDirectoryName(fileName);
                if (!Directory.Exists(path))
                    Directory.CreateDirectory(path);
                using (FileStream fs = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Write))
                {
                    using (StreamWriter sw = new StreamWriter(fs, encoding))
                    {
                        sw.Write(content);
                        sw.Close();
                    }
                    fs.Close();
                }
            }
            catch (Exception ex)
            {
                LogHelper.Write("Wind.Compressor.Business.Utils.WriteFile error", ex);
            }
        }

        public static bool CopyFile(string fileRootPath, string fileMidPath, string fullName, string destPath)
        {
            bool result = false;
            string sourceFileName = string.Empty;
            string destFileName = string.Empty;

            try
            {
                if (!fileRootPath.EndsWith("\\"))
                    fileRootPath += "\\";
                if (!fileMidPath.EndsWith("\\"))
                {
                    if (!string.IsNullOrEmpty(fileMidPath))
                        fileMidPath += "\\";
                }
                if (!destPath.EndsWith("\\"))
                    destPath += "\\";

                if (!Directory.Exists(destPath + fileMidPath))
                {
                    Directory.CreateDirectory(destPath + fileMidPath);
                }
                sourceFileName = fileRootPath + fileMidPath + fullName;
                destFileName = destPath + fileMidPath + fullName;

                File.Copy(sourceFileName, destFileName, true);
                result = true;
            }
            catch (Exception ex)
            {
                LogHelper.Write("Wind.Compressor.Business.Utils.CopyFile error." + destFileName, ex);
            }
            return result;
        }
        #endregion
    }
}
